//
//  NumberViewController.m
//  QT
//
//  Created by 甘世清 on 2024/10/15.
//

#import "NumberViewController.h"
#import <JHVerificationCodeView/JHVerificationCodeView.h>
@interface NumberViewController ()
@property (nonatomic , strong) JHVerificationCodeView *codeView;
@property (nonatomic , strong) SQCustomButton * rightBtn;
@end

@implementation NumberViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self SQ_Header];
}

- (void)SQ_UIViewForSelfView{
    self.view.backgroundColor = SQColor(@"#F7F7F7");
    UILabel * label = [[UILabel alloc]init];
    label.text = SQNSLocalString(@"输入验证码");
    label.font = FONT_REGULAR(22);
    label.textColor = SQColor333333;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(self.view).offset(100+TopHeight);
    }];
    
    UILabel * label1 = [[UILabel alloc]init];
    label1.text = [NSString stringWithFormat:@"%@%@",SQNSLocalString(@"已发送验证码至"),self.phoneString];
    label1.font = FONT_REGULAR(10);
    label1.textColor = SQColor999999;
    [self.view addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(30);
        make.top.equalTo(label.mas_bottom).offset(10);
    }];
    
    [self.view addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view);
        make.centerY.equalTo(label1);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(30);
    }];
    WS(weakSelf);
    [self.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        if ([button.text isEqualToString:SQNSLocalString(@"重新获取")]) {
            [weakSelf SQ_Header];
        }
    }];
    
    [self.view addSubview:self.codeView];
    self.codeView.finishBlock = ^(JHVerificationCodeView *codeView, NSString *code) {
        [weakSelf SQ_login:code];
    };
    
}
-(void)SQ_login:(NSString*)code{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.phoneString forKey:@"phone"];
    [dic setObject:code forKey:@"code"];
    if ([[Manager SQ_stringForKey:@"hexToken"] isNotEmpty]) {
        [dic setObject:[Manager SQ_stringForKey:@"hexToken"] forKey:@"deviceToken"];
    }
    WS(weakSelf);
    [HttpTool post:@"user/login" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        UserModel * model = [UserModel mj_objectWithKeyValues:httpModel.data];
        [UserModel updateLocalUserModel:model];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[Manager SQ_getViewController]];
        weakSelf.view.window.rootViewController = nav;
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

- (void)SQ_Header{
    
    WS(weakSelf);
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setObject:self.phoneString forKey:@"phone"];
    [HttpTool post:@"user/verify" paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        [weakSelf SQ_showBtn];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        self.rightBtn.text = SQNSLocalString(@"重新获取");

        [YJProgressHUD  showMsgWithoutView:httpModel.message];
    }];
}

-(void)SQ_showBtn{
    WS(weakSelf);
    [LSTTimer addTimerForTime:60 identifier:@"" handle:^(NSString * _Nonnull day, NSString * _Nonnull hour, NSString * _Nonnull minute, NSString * _Nonnull second, NSString * _Nonnull ms) {
         weakSelf.rightBtn.text =[NSString stringWithFormat:@"%@ s",second];
        } finish:^(NSString * _Nonnull identifier) {
         weakSelf.rightBtn.text = SQNSLocalString(@"重新获取");
        } pause:^(NSString * _Nonnull identifier) {
            
        }];
}

- (SQCustomButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(0, 0) midmargin:0];
        _rightBtn.textColor = SQColor(@"#999999");
        _rightBtn.text = @"60";
        _rightBtn.font = FONT_REGULAR(10);
    }
    return _rightBtn;
}

- (JHVerificationCodeView *)codeView{
    if (!_codeView) {
        JHVCConfig *config     = [[JHVCConfig alloc] init];
        config.inputBoxNumber  = 6;
        config.inputBoxSpacing = 10;
        config.inputBoxWidth   = 44;
        config.inputBoxHeight  = 44;
        config.tintColor       =  UIColor.lightGrayColor;
         config.secureTextEntry = NO;
        config.inputBoxColor   = SQColor(@"#666666");
        config.font            = FONT_MEDIUM(14);
        config.textColor       = SQColor333333;
        config.inputType       = JHVCConfigInputType_Number;
        config.autoShowKeyboard = YES;
        config.inputBoxBorderWidth  = 2;
        config.showUnderLine = NO;
        config.keyboardType = UIKeyboardTypeNumberPad;
        config.finishFonts = @[FONT_MEDIUM(14),FONT_MEDIUM(14)];
        config.finishTextColors = @[UIColor.lightGrayColor,UIColor.lightGrayColor];
        _codeView = [[JHVerificationCodeView alloc]initWithFrame:CGRectMake(0, 180+TopHeight, SCREEN_WIDETH, 44) config:config];
    }
    return _codeView;
}

@end
